package org.itboys.core.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mongo.SundayVillage;
import org.itboys.core.util.NumberUtil;
import org.itboys.core.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/1
 * @Description：仓库_service
 */
@Service
public class SundayStoreService extends BaseMongoService<SundayStore> {
    @Autowired
    private SundayUploadService uploadService;
    @Resource(name = "mobileDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<SundayStore> getEntityClass() {
        return SundayStore.class;
    }

    @Autowired
    private SundayVillageService villageService;

    public void save(Long id,SundayStore store,MultipartFile imageFile)throws Exception{
        if(!PhoneUtil.isMobileNumber(store.getMobile())){
            throw new ErrorException("手机格式错误");
        }
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            store.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,store);

        }else{
            store.setNumber(NumberUtil.generateNumber("STORE"));
            super.save(store);
        }
    }

    /**
     * web端分页获取
     * 精确查询根据省市区id
     * 模糊查询字段为名称
     * @param request
     * @return
     */
    public PageResult<SundayStore> selectAll(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request,"provinceId","cityId","districtId","type");
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name","number");
        param.put("isDeleted", SundayCommonConstants.NO);

        return super.containpageQuery(request, vagueParam, param);
    }
}
